/* eslint-disable prefer-rest-params */
module.exports = (function (win) {
    const events = {
        dir: new (require('./events/dir/index'))(),
        file: new (require('./events/file'))(),
        os: new (require('./events/os'))(),
        request: new (require('./events/request'))(),
        command: new (require('./events/command'))(),
        spawn: new (require('./events/spawn'))(),
        mysql: new (require('./events/mysql'))(),
        dialog: new (require('./events/dialog'))(win),
        // 以下是 插件（使用由 node 和 electron 之外的代码实现的内容）
        excel: new (require('./plugin/excel'))(),
        gdal_tool: new (require('./plugin/gdal_tool/index'))(),
    };

    for (const i in events) {
        events[i].events = events;
    }

    /**
     * obj 格式为
     *      event : "mysql-isInit",
     *      param : {}  //根据每个函数具体定义
     * event 为 ipc 事件对象
     *
     * 前端接收到的信息必定是一个 错误对象和相应的内容，错误对象为空时，可能就没有相应的内容
     * */
    return function (event, obj) {
        let eventList = [];
        if (obj.event) {
            eventList = obj.event.toString().split('-');
        }
        if (eventList.length === 2) {
            events[eventList[0]][eventList[1]].bind(events[eventList[0]],
                (function (id) {
                    const data = arguments;
                    const async = data[3] ? 'fetchBackAsync' : 'fetchBack';
                    event.sender.send(async, {
                        id,
                        data: data[2],
                        err: data[1],
                        status: data[3],
                    });
                }).bind(null, obj.id),
            )(obj.param || {});
        }
    };
});
